<?php

declare(strict_types=1);

namespace App\Request\Orders;

use App\Constants\Order\OrderCode;
use App\Request\FormRequest;
use Hyperf\Collection\Arr;
use Hyperf\Validation\Rule;

/**
 * Class Order
 * @author zx
 * @package App\Request\Orders
 */
class Order extends FormRequest
{
    /**
     * 场景
     * @var array|\string[][]
     */
    protected array $scenes = [
        'save' => [
            'merchant_no',
            'consignee',
            'mobile',
            'prov',
            'city',
            'area',
            'address',
            'paid_amount',
            'postage_amount',
            'discount_amount',
            'user_note',
            'goods',
            'goods.*.product_id',
            'goods.*.paid_price',
            'goods.*.num',
            'goods.*.snapshot',
            'goods.*.product_link',
            'time_expire',
            'attach',
            'source',
            'jump',
            'open_id',
            'app_id',
            'payer',
            'payer.name',
            'payer.id_card',
            'payer.mobile',
            'appid'
        ],
        'refund' => [
            'merchant_no',
            'no',
            'refund_no',
            'user_note',
            'consignee',
            'mobile'
        ],
        'read' => [
            'merchant_no',
            'no',
        ],
        'delivery' => [
            'delivers',
            'delivers.*.shipping_code',
            'delivers.*.no',
            'delivers.*.logistics_no'
        ],
        'logistics' => [
            'logistics_code',
            'logistics_no'
        ]
    ];

    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        $rules = [
            'merchant_no' => [
                'required',
                'alpha_dash',
                'max:50'
            ],
            'refund_no' => [
                'required',
                'alpha_dash',
                'max:50',
                Rule::unique('payment', 'no')
            ],
            'consignee' => 'required|max:50',
            'mobile' => 'required|mobile',
            'prov' => 'required|alpha|max:50',
            'city' => 'required|alpha|max:50',
            'area' => 'required|alpha|max:50',
            'address' => 'required|max:100',
            'paid_amount' => 'required|numeric|min:0|max:30000',
            'postage_amount' => 'required|numeric|min:0',
            'discount_amount' => 'nullable|array|min:1',
            'discount_amount.coupon' => 'required_without_all:discount_amount.discount,discount_amount.integral|numeric|min:0',
            'discount_amount.discount' => 'numeric|min:0',
            'discount_amount.integral' => 'numeric|min:0',
            'user_note' => 'max:255',
            'goods' => 'required|array',
            'goods.*.product_id' => 'required|alpha_dash|max:40',
            'goods.*.paid_price' => 'required|numeric|min:0.01',
            'goods.*.num' => 'required|integer|min:1',
            'goods.*.discount_amount' => 'numeric|min:0',
            'goods.*.snapshot' => 'required|url|max:255',
            'goods.*.product_link' => 'required|url|max:255',
            'no' => 'required_without:merchant_no|alpha_dash|max:50',
            'delivers' => 'required|array',
            'delivers.*.shipping_code' => 'required|alpha|max:15',
            'delivers.*.no' => 'required|alpha_dash|max:50',
            'delivers.*.logistics_no' => 'required|alpha_dash|max:50',
            'logistics_code' => 'required|alpha_dash|max:20',
            'logistics_no' => 'required|max:50',
            'time_expire' => 'date|date_format:Y-m-d H:i:s|after_or_equal:'.date('Y-m-d H:i:s', strtotime('+5 minutes')).'|before_or_equal:'.date('Y-m-d H:i:s', strtotime('+24 hours')),
            'attach' => 'max:255',
            'source' => [
                'max:25',
                Rule::in(OrderCode::$sourceMap)
            ],
            'jump' => 'boolean',
            'open_id' => [
                Rule::requiredIf(fn() => Arr::has($this->all(), 'jump') && !$this->input('jump')),
                'max:50'
            ],
            'appid' => [
                Rule::requiredIf(fn() => Arr::has($this->all(), 'jump') && !$this->input('jump')),
                'max:50'
            ],
            'payer' => [
                Rule::requiredIf(fn() => Arr::has($this->all(), 'jump') && !$this->input('jump')),
                'array'
            ],
            'payer.name' => [
                Rule::requiredIf(fn() => Arr::has($this->all(), 'jump') && !$this->input('jump')),
                'max:10'
            ],
            'payer.id_card' => [
                Rule::requiredIf(fn() => Arr::has($this->all(), 'jump') && !$this->input('jump')),
                'id_card',
            ],
            'payer.mobile' => [
                Rule::requiredIf(fn() => Arr::has($this->all(), 'jump') && !$this->input('jump')),
                'mobile'
            ]
        ];

//        if (in_array(getApp(), ['api'])) {
//            $rules['merchant_no'][] = Rule::unique('order')->where('shop_id', getShop('id'));
//        }
        if (in_array($this->getAction(), ['refund', 'read'])) {
            $rules['merchant_no'] = 'required_without:no|alpha_dash|max:50';
        }

        return $rules;
    }
}
